﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Dependencies;
using Castle.Windsor;

namespace SampleApp.MvcBackend.Services
{
	public class WebApiDependencyResolver : IDependencyResolver
	{
		readonly IWindsorContainer container;

		public WebApiDependencyResolver( IWindsorContainer container )
		{
			this.container = container;
		}

		public IDependencyScope BeginScope()
		{
			return this;
		}

		public object GetService( Type serviceType )
		{
			if ( this.container.Kernel.HasComponent( serviceType ) )
			{
				return this.container.Resolve( serviceType );
			}
			else
			{
				return null;
			}
		}

		public IEnumerable<object> GetServices( Type serviceType )
		{
			if ( this.container.Kernel.HasComponent( serviceType ) )
			{
				return ( IEnumerable<Object> )this.container.ResolveAll( serviceType );
			}

			return new List<object>();
		}

		public void Dispose()
		{

		}
	}
}